//
//  ZFK_tagView.m
//  Drops
//
//  Created by SHG on 16/5/23.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "ZFK_tagView.h"
#import "ZFK_tagCollectionViewCell.h"
#import "ZFK_tags.h"
#import "UIKit+AFNetworking.h"

@implementation ZFK_tagView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(sHEIGHT / 7, sHEIGHT / 7);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, sWIDTH, sHEIGHT / 6) collectionViewLayout:layout];
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    
    [self addSubview:self.mainCollectionView];
    
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    
    [self.mainCollectionView registerClass:[ZFK_tagCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ZFK_tagCollectionViewCell class])];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    NSLog(@"------------%ld", self.tagsArr.count);
    return self.tagsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZFK_tagCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZFK_tagCollectionViewCell class]) forIndexPath:indexPath];
    ZFK_tags *tags = [[ZFK_tags alloc] init];
    [tags setValuesForKeysWithDictionary:self.tagsArr[indexPath.item]];
    [cell.imageView setImageWithURL:[NSURL URLWithString:tags.imgUrl]];
    cell.tagLabel.text = [NSString stringWithFormat:@"#%@", tags.tagText];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"tag被点击:%ld", indexPath.row);
}

- (void)setTagsArr:(NSMutableArray *)tagsArr {
    _tagsArr = tagsArr;
    [self.mainCollectionView reloadData];
}

@end
